package com.start_eternity.oasissafewalk.controller;


import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import com.start_eternity.oasissafewalk.mapper.DriverMapper;
import com.start_eternity.oasissafewalk.mapper.ParentMapper;
import com.start_eternity.oasissafewalk.pojo.Driver;
import com.start_eternity.oasissafewalk.pojo.Parent;
import com.start_eternity.oasissafewalk.utils.AppConfigTools;
import com.start_eternity.oasissafewalk.utils.JwtUtils;
import com.start_eternity.oasissafewalk.utils.WechatTools;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    /**
     * 盐值：混淆密码
     */
    private static final String SALT = "GGBo";

    @Autowired
    WechatTools wechatTools;

    @Resource
    private ParentMapper parentMapper;

    @Resource
    private DriverMapper driverMapper;
    /**
     * 微信小程序的登陆，如果有账号，则返回登陆成功，如果没有则返回（首次用户，需要认证)
     *Dependency injection frameworks such as Spring support dependency injection by using annotations such as @Inject and @Autowired. These annotations can be used to inject beans via constructor, setter, and field injection.
     * Generally speaking, field injection is discouraged. It allows the creation of objects in an invalid state and makes testing more difficult. The dependencies are not explicit when instantiating a class that uses field injection.
     * In addition, field injection is not compatible with final fields. Keeping dependencies immutable where possible makes the code easier to understand, easing development and maintenance.
     * Finally, because values are injected into fields after the object has been constructed, they cannot be used to initialize other non-injected fields inline.
     * This rule raises an issue when the @Autowired or @Inject annotations are used on a field.
     * @return
     */
    @PostMapping("/getWxInfoTest")
    public String getWxInfoTest(@RequestBody JSONObject obj) {
        String AppId = AppConfigTools.getWxAppId();
        String AppSecret = AppConfigTools.getWxAppSecret();
        JSONObject wxJson = JSONObject.parseObject(WechatTools.getOpenid(obj.getString("code"), AppId, AppSecret));
        // 获取openid
        String openid = wxJson.getString("openid");
        //2.对Openid进行加密
        String encryptOpenid = DigestUtils.md5DigestAsHex((SALT + openid).getBytes());
        String jsonString = JwtUtils.generateWxJwt();
        log.info("token的返回值{}", jsonString);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("token", jsonString);
        if(openid != null) {
            Parent parent = parentMapper.selOpenId(encryptOpenid);
            if (parent != null) {
                jsonObject.addProperty("pId", parent.getPId());
                return jsonObject.toString();
            }
            Parent parent1 = new Parent();
            parent1.setOpenId(encryptOpenid);
            int insert = parentMapper.insert(parent1);
            if (insert > 0) {
                return jsonObject.toString();
            }
            return null;
        }
        return null;
    }

    @PostMapping("/getWxDriverInfo")
    public String getWxDriverInfo(@RequestBody JSONObject obj) {
        String AppId = AppConfigTools.getWxAppId();
        String AppSecret = AppConfigTools.getWxAppSecret();
        JSONObject wxJson = JSONObject.parseObject(WechatTools.getOpenid(obj.getString("code"), AppId, AppSecret));
        // 获取openid
        String openid = wxJson.getString("openid");
        //2.对Openid进行加密
        String encryptOpenid = DigestUtils.md5DigestAsHex((SALT + openid).getBytes());
        String jsonString = JwtUtils.generateWxJwt();
        log.info("token的返回值{}", jsonString);
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("token", jsonString);
        if(openid != null) {
            Driver driver = driverMapper.selOpenId(encryptOpenid);
            if (driver != null) {
                jsonObject.addProperty("pId", driver.getDriverId());
                return jsonObject.toString();
            }
            Driver driver1 = new Driver();
            driver1.setOpenId(encryptOpenid);
            int insert = driverMapper.insert(driver1);
            if (insert > 0) {
                return jsonObject.toString();
            }
            return null;
        }
        return null;
    }
}